package copy.express.process.spit.impl;


import copy.express.pojo.Node;
import copy.express.pojo.NodeType;
import copy.express.pojo.type.Type;
import copy.express.process.spit.ConfirmTypeEndIndex;
import copy.tools.StringStrTools;

public  class FieldTypeEndIndex implements ConfirmTypeEndIndex {
    //获取三个单例解析器
    private StringTypeEndIndex stringTypeEndIndex = StringTypeEndIndex.getInstance();
    private SymbolTypeEndIndex symbolTypeEndIndex = SymbolTypeEndIndex.getInstance();
    private NumberTypeEndInex numberTypeEndInex = NumberTypeEndInex.getInstance();

    private static FieldTypeEndIndex fieldTypeEndIndex = new FieldTypeEndIndex();
    public FieldTypeEndIndex() {
    }
    public static ConfirmTypeEndIndex getInstance(){
        return fieldTypeEndIndex;
    }

    /**
     * 判断依据: 排除法
     * @param c
     * @return
     */

    @Override
    public boolean confirmType(char c) {
        if (c == '$')return true;
        //排除String
        if ( stringTypeEndIndex.confirmType(c) == false ){
            //排除Symbol
            if ( symbolTypeEndIndex.confirmType(c) == false ){

                return true;

            }
        }

        return false;
    }

    @Override
    public int findEndIndex(int start,int pos, char[] chars) {
        //已经越界了.返回
        if (pos==chars.length){
            return pos;
        }
        //如果是逗号

        if ( chars[pos] == ' ' || chars[pos] == ','  )return pos;
        //这是一个字段字符串
        if ( confirmType(chars[pos])  ){
            return findEndIndex(start,pos+1,chars);
        }else{

            //这不是一个字段字符串subString位置: start-1,pos
            return pos;
        }
    }

    @Override
    public  NodeType createNode(int start, int end, char[] chars){
        String result = StringStrTools.appendFromStartToEnd(start,end-1,chars);
        if (result.equals("true") || result.equals("false") ){
            return new Node(result, Type.Boolean.DEFAULT);
        }else if (result.equals("$")){
            return new Node(result,Type.FieldType.SELF);
        }else{
            return new Node(result, Type.FieldType.DEFAULT);
        }
    }
}
